package com.gljx.web.controller.back.power;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gljx.util.CommonUtil;
import com.gljx.web.Entity.Role;
import com.gljx.web.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleAction {

	private  Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private RoleService roleService;
	/**
	 * 进入角色管理页面
	* @return
	* @author LiXiang  @date 2017年10月30日 下午4:04:06
	 */
	@RequestMapping("/look.action")
    public ModelAndView look() {
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/backManage/power/role_list");
		return mav;
	}
	/**
	 * 角色管理分页
	* @param rows
	* @param page
	* @param roleName
	* @param fisDelete
	* @return
	* @author LiXiang  @date 2017年10月31日 上午10:31:36
	 */
	@RequestMapping("/list.action")
	@ResponseBody
    public Map <String,Object > list(String rows, 
							String page, 
							String roleName, 
							String fisDelete) {
		if(CommonUtil.isSpecialChar_simple(roleName)){
			Map <String,Object > dataMap = new HashMap<String, Object>();
			logger.error("##自定义异常##:"+SecurityUtils.getSubject().getPrincipal()+">>>非法输入");
			dataMap.put("rows",new ArrayList<>());
			dataMap.put("total",0);
			return dataMap;
		}
		//转换 属性为数据库可接收属性
		fisDelete = CommonUtil.isConstant(fisDelete,"0","1");
		return roleService.selectRoleByProject(rows, page, roleName, fisDelete);
		
	}
	/**
	 * 添加角色
	* @param roleName
	* @param roleCode
	* @param remark
	* @return
	* @author LiXiang  @date 2017年10月31日 上午10:31:46
	 */
	@RequiresPermissions("/role/add.action")
	@RequestMapping("/add.action")
	@ResponseBody
    public Map <String,Object > add(String roleName,
    								String roleCode,
    								String remark) {
		Map <String,Object > dataMap = new HashMap<String, Object>();
		//校验字符
		if(CommonUtil.isSpecialChar_simple(roleName,roleCode,remark)||
				"admin_role".equals(roleName)){
			logger.error("##自定义异常##:"+SecurityUtils.getSubject().getPrincipal()+">>>非法输入");
			dataMap.put("error", "非法字符");
			return dataMap;
		}
		//插入
		try {
			dataMap.put("success",roleService.insertRole(roleName, 
											roleCode, 
											CommonUtil.nowTime(), 
											(String)SecurityUtils.getSubject().
													getPrincipal(), 
											remark, 0).get("v_result"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataMap;
	}
	
	/**
	 * 获取角色完整信息
	* @param id
	* @return
	* @author LiXiang  @date 2017年10月30日 下午1:50:46
	 */
	@RequestMapping("/roledata.action")
	@ResponseBody
    public Map <String,Object > roledata(String id) {
		Map <String,Object > dataMap = new HashMap<String, Object>();
		dataMap.put("role",roleService.selectByPrimaryKey(Integer.valueOf(id)));
		return dataMap;
	}
	
	/**
	 * 修改
	* @return
	* @author LiXiang  @date 2017年10月30日 下午2:00:38
	 * @throws Exception 
	 */
	@RequestMapping("/update.action")
	@RequiresPermissions("/role/update.action")
	@ResponseBody
    public Map <String,Object > update(Role role) {
		Map <String,Object > dataMap = new HashMap<String, Object>();
		try {
			dataMap.put("success", roleService.updateByPrimaryKeySelective(role));
		} catch (DuplicateKeyException e) {
			dataMap.put("success",0);
		} catch (Exception e){
			dataMap.put("success",-1);
		}
		
		return dataMap;
	}
	
	@RequiresPermissions("/role/setdelete.action")
	@RequestMapping("/setdelete.action")
	@ResponseBody
    public Map <String,Object > setdelete(String ids,String isDelete){
		Map <String,Object > dataMap = new HashMap<String, Object>();
		try {
			dataMap.put("success",roleService.setdelete(ids, isDelete));
		} catch (Exception e) {
			dataMap.put("erroe","");
			logger.error("#角色操作失败#---停用启用"+e);
		}
		return dataMap;
	}
}
